include etc/glee.mk
include etc/run.mk

X      = glee1
Glees := $(shell cd $(Glee); $(Gawk) -f $(Etc)/uses.awk ${X}.glee)
Awks  := $(subst .glee,.awk,$(Glees))
Src   := $(Glee)/$(subst .glee ,.glee $(Glee)/,$(Glees))  
F      = -f $(subst .awk ,.awk -f ,$(Awks))

Run    = AWKPATH=$(Awk) $(Gawk) $F
Debug  = AWKPATH=$(Awk) $(Spy)  $F

#### main
compile : src ready commentouts swaps latest older

#### checking for the right files and dir
src     : $(Src)
ready   : files dirs 
files   : $(Gawk) $(Pgawk)
dirs    : 
	@mkdir -p $(Dirs)

#### add comment characters
commentouts :
	@$(foreach f, $(Glees), \
        cd $(Glee); \
        $(Gawk) -f $(Etc)/commentout.awk $f > $(Tmp)/$(subst .glee,.awk,$f); )

#### expand the swaps
swaps :
	@cd $(Tmp) ; $(Gawk) -f $(Etc)/swaps.awk Dir=$(Awk) $(Awks)

#### cat all files into one 
Latest = $(App)/$X
latest :
	@echo "#!$(Gawk) -f" > $(Latest)
	@printf "\n# $(X), build #$(Build), by '$(USER)' on `date`.\n\n" >> $(Latest)
	@cat etc/header >> $(Latest)
	@$(foreach f,$(Awks), \
         printf "\n#===| $f |=======\n\n" >> $(Latest);  \
         cat $(Awk)/$f >> $(Latest); )
	@chmod +x $(Latest)
	@ls -lsa $(Latest)

#### copy the latest into the older folder 
Older = $(Old)/$(X).$(Build)
older :
	@cp $(Latest) $(Older)
